﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using App.Data;
using App.Core.Extensions;


public partial class Pages_Public_ProjectDetails : BasePage
{
    protected int _ProjectID;    
    protected ProjectFile _ProjectFile = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindProjectDeatils();
        }
    }
    protected void BindPageInfo()
    {
        _ProjectID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        Page.Title = WebUtil.GetPageTitle("Project Details");
    }
    protected void BindProjectDeatils()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        Project project = context.Projects.FirstOrDefault(P => P.ID == _ProjectID);
        _ProjectFile = project.ProjectFiles.FirstOrDefault(P => P.FileTypeID != ConfigReader.ProjectBrocheureFileTypeID);        
        if (project == null)
            ShowErrorMessage();
        else
        {
            h4Heading.InnerHtml = String.Format("{0} Details", project.PropertyType.Name.HtmlEncode());
            String table = @"<table cellpadding='3' cellspacing='0' style='width:100%;'>
                                <colgroup>
                                    <col style='width:20%;' />
                                    <col />
                                </colgroup>
                                {0}
                            </table>";
            StringBuilder sb = new StringBuilder();
            ///Bind General Information
            sb.AppendFormat("<tr><td>Project Name</td> <td>{0}</td></tr>", project.Name.HtmlEncode());
            sb.AppendFormat("<tr><td>Status</td> <td>{0}</td></tr>", project.ProjectStatus.Name);
            sb.AppendFormat("<tr><td>Description</td> <td>{0}</td></tr>", WebUtil.FormatText(project.Description));
            
            ltrGeneralInformation.Text = String.Format(table, sb.ToString());
            sb.Length = 0;

            ///Bind Location
            sb.AppendFormat("<tr><td>City</td> <td>{0}</td></tr>", project.Zone.City.Name.HtmlEncode());
            sb.AppendFormat("<tr><td>Zone</td> <td>{0}</td></tr>", project.Zone.Name.HtmlEncode());

            if(!project.StreetAddress.IsNullOrEmpty())
                sb.AppendFormat("<tr><td>Street Address</td> <td>{0}</td></tr>", project.StreetAddress.HtmlEncode());
            if(!project.HouseNo.IsNullOrEmpty())
                sb.AppendFormat("<tr><td>House</td> <td>{0}</td></tr>", project.HouseNo.HtmlEncode());
            if(!project.AddressHint.IsNullOrEmpty())
                sb.AppendFormat("<tr><td>Address Hint</td> <td>{0}</td></tr>", WebUtil.FormatText(project.AddressHint));
            ltrLocation.Text = String.Format(table, sb.ToString());
            sb.Length = 0;
            
            ///Bind Specifications
            sb.AppendFormat("<tr><td>Size ({0})</td> <td>{1}</td></tr>", project.Unit.Name, WebUtil.FormatData(project.Area));

            if(!project.AreaOther.IsNullOrEmpty())
                sb.AppendFormat("<tr><td>Other Aavailable Sizes</td> <td>{0}</td></tr>", project.AreaOther.HtmlEncode());
            
            if (project.PropertyTypeID != ConfigReader.LandPropertyTypeID)
            {                
                sb.AppendFormat("<tr><td>Number Of Units</td> <td>{0}</td></tr>", project.NoOfUnits);
                sb.AppendFormat("<tr><td>Number of Beds</td> <td>{0}</td></tr>", project.NoOfBeds);
                sb.AppendFormat("<tr><td>Number Of Floors</td> <td>{0}</td></tr>", project.NoOfFloors);
                sb.AppendFormat("<tr><td>Available Units</td> <td>{0}</td></tr>", project.AvailableUnits);

                sb.AppendFormat("<tr><td>Has Parking?</td> <td>{0}</td></tr>", project.HasParking ? "Yes" : "No");
                sb.AppendFormat("<tr><td>Has Generator?</td> <td>{0}</td></tr>", project.HasGenerator ? "Yes" : "No");
                sb.AppendFormat("<tr><td>Has Lift?</td> <td>{0}</td></tr>", project.HasLift ? "Yes" : "No");
                sb.AppendFormat("<tr><td>Has Security?</td> <td>{0}</td></tr>", project.HasSecurity ? "Yes" : "No");
            }
            ltrSpecification.Text = String.Format(table, sb.ToString());
            sb.Length = 0;

           ///Bind pricing
            if (project.PricePerUnitFrom > 0 && project.PricePerUnitTo > 0 & project.ShowPricingInfo)
                sb.AppendFormat("<tr><td>Price Per {0}</td> <td>From {1} To {2}</td></tr>", project.Unit.Name, WebUtil.FormatData(project.PricePerUnitFrom), WebUtil.FormatData(project.PricePerUnitTo));
            else
                sb.AppendFormat("<tr><td>Price Per {0}</td> <td>Negotiatable</td></tr>", project.Unit.Name);
            sb.AppendFormat("<tr><td>Booking Price</td> <td>{0}</td></tr>", project.BookingFee > 0 ? WebUtil.FormatData(project.BookingFee) : "Negotiatable");
            sb.AppendFormat("<tr><td>Booking Available?</td> <td>{0}</td></tr>", project.HasBookingAvailable ? "Yes" : "No");
            ltrPricing.Text = String.Format(table, sb.ToString());
            sb.Length = 0;

            ///Bind Contact person
            foreach (ProjectContact projectContact in project.ProjectContacts)
            {
                sb.AppendFormat("<tr><td style='background-color:#ECECEC'>Name</td> <td style='background-color:#ECECEC'>{0}</td></tr>", projectContact.Contact.Name.HtmlEncode());
                sb.AppendFormat("<tr><td>Email</td> <td>{0}</td></tr>", projectContact.Contact.Email.HtmlEncode());
                if(!projectContact.Contact.Phone.IsNullOrEmpty())
                    sb.AppendFormat("<tr><td>Phone</td> <td>{0}</td></tr>", projectContact.Contact.Phone.HtmlEncode());
            }
            ltrContactPerson.Text = String.Format(table, sb.ToString());
        }
    }
    protected void ShowErrorMessage()
    {
        WebUtil.ShowMessageBox(divMessage, "Requested Project was not found.", true);
        pnlFormContainer.Visible = false;
    }
}